package com.coocaa.ops.admin.v1.auth.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysMenuBiz;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysMenuRunBiz;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysRunBiz;
import com.coocaa.ops.admin.v1.auth.sys.model.query.SysMenuModel;
import com.coocaa.ops.admin.v1.auth.sys.service.ISysMenuService;
import com.coocaa.ops.admin.v1.auth.sys.util.SysMenuValidator;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.auth.entity.SysMenuEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysMenuRunEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysRunEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tangminghong
 * @date : 2020-06-11
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService {
    @Autowired
    private ISysMenuBiz sysMenuBiz;
    @Autowired
    private ISysRunBiz sysRunBiz;
    @Autowired
    private ISysMenuRunBiz sysMenuRunBiz;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 获取菜单没有的权限信息
     *
     * @param menuId
     * @return
     */
    @Override
    public Map getNotRunByMenuId(Integer menuId) {
        List<SysRunEntity> runList = sysRunBiz.getMapper().listNotRunByMenuId(menuId);
        return R.ok(runList);
    }

    /**
     * 获取菜单已有的权限信息
     *
     * @param menuId
     * @return
     */
    @Override
    public Map getRunByMenuId(Integer menuId) {
        List<SysRunEntity> runList = sysRunBiz.getMapper().listRunByMenuId(menuId);
        return R.ok(runList);
    }

    /**
     * 保存 菜单的操作权限信息
     *
     * @param menuId
     * @param runIds
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveMenuRuns(Integer menuId, Integer[] runIds) {
        List<SysMenuRunEntity> entitys = new ArrayList(runIds == null ? 0 : runIds.length);
        if (ArrayUtils.isNotEmpty(runIds)) {
            for (Integer runId : runIds) {
                SysMenuRunEntity entity = new SysMenuRunEntity();
                entity.setMenuId(menuId);
                entity.setRunId(runId);
                entitys.add(entity);
            }
        }
        sysMenuRunBiz.saveBatch(menuId, entitys);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysMenuEntity menuEntity) {
        SysMenuValidator.valid(menuEntity);
        if (null == menuEntity.getId()) {
            addOrUpdateByMenuToken(menuEntity);
        } else {
            sysMenuBiz.updateById(menuEntity);
        }
    }

    @Override
    public SysMenuEntity getDetailInfo(Integer id) {
        SysMenuEntity query = sysMenuBiz.queryById(id);
        query.setId(query.getId());
        return query;
    }

    @Override
    public List<SysMenuEntity> queryAll(SysMenuModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        List<SysMenuEntity> list = sysMenuBiz.getMapper().queryCommonPageList(params);
        return list;
    }

    @Override
    public Map<String, Object> getPageResult(SysMenuModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<SysMenuEntity> pageResult = sysMenuBiz.getCommonPageList(params, false);
        return R.ok(pageResult);
    }

    /**
     * 更新排序
     *
     * @param id
     * @param seq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSeq(Integer id, Integer seq) {
        SysMenuEntity update = new SysMenuEntity();
        update.setId(id);
        seq = (seq == null ? Integer.valueOf(0) : seq);
        update.setSeq(seq);
        sysMenuBiz.updateById(update);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        String result = this.deleteBatch(ids, user.getId());
        if (R.SUCCESS.equals(result)) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(result);
        }
    }

    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        SysMenuServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }
    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    private String deleteBatch(Integer[] ids, Integer userId) {
        if (ArrayUtils.isNotEmpty(ids)) {
            UpdateWrapper<SysMenuEntity> wrapper = new UpdateWrapper<>();
            wrapper.in("id", ids);
            SysMenuEntity update = new SysMenuEntity();
            update.setDisabled(2);
            sysMenuBiz.getMapper().update(update, wrapper);
        }
        return R.SUCCESS;
    }

    private void addOrUpdateByMenuToken(SysMenuEntity menu) {
        SysMenuEntity oldMenu = sysMenuBiz.getMenuByToken(menu.getMenuToken());
        if (oldMenu == null) {
            sysMenuBiz.add(menu);
        } else {
            menu.setId(oldMenu.getId());
            menu.setDisabled(0);
            menu.setFlag(1);
            sysMenuBiz.updateById(menu);
        }
    }
}
